package com.admission.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.admission.dao.ProvinceScoreDao;
import com.admission.dao.RecruitmentDetailDao;
import com.admission.model.Province;
import com.admission.model.ProvinceScore;
import com.admission.util.ArrayUtils;
import com.admission.util.EnrollUtils;
import com.admission.util.ExcelUtils;
import com.admission.util.StaticRecruitment;
@Repository
@Transactional
public class ProvinceScoreManager {
	private ProvinceScoreDao provinceScoreDao;
	private ProvinceManager provicneManager;
	private ArrayUtils arrayUtils;
	private StaticRecruitment staticRecruitment;
	private RecruitmentDetailDao recruitmentDetailDao;
	private ServletContext servletContext;
	/**
	 * 通过id查找唯一对象
	 * 
	 * @param id
	 * @return
	 */
	public ProvinceScore getById(String id) {
		return provinceScoreDao.findUniqueBy("id", id);
	}

	/**
	 * 保存
	 * 
	 * @throws IOException
	 */
	public void save(String excelPath,String ArtId) throws Exception {
		Workbook workbook = ExcelUtils.getCommonWorkbook(excelPath);
		Sheet sheet = workbook.getSheetAt(0);
		Province c=provicneManager.getById(ArtId);
		for (int j = 1; j <= sheet.getLastRowNum(); j++) {
			ProvinceScore rd = new ProvinceScore();
			Row row1 = sheet.getRow(j);
			rd.setSequence(EnrollUtils.getCellFormatValue(row1.getCell(0)));
			rd.setYear(EnrollUtils.getCellFormatValue(row1.getCell(1)));
			rd.setProvince((String) arrayUtils.getKey(
					arrayUtils.getProvinceMap(),
					EnrollUtils.getCellFormatValue(row1.getCell(2))));
			rd.setType((String) arrayUtils.getKey(arrayUtils.getType(),
					EnrollUtils.getCellFormatValue(row1.getCell(3))));
			if (StringUtils.isEmpty(rd.getProvince())) {
				throw new Exception("省份录取批次线表格第" + (j + 1) + "行的省份有错误，省份录取批次线表格提交失败");
			}
			if (StringUtils.isEmpty(rd.getType())) {
				throw new Exception("省份录取批次线表格第" + (j + 1) + "行的类型有错误，省份录取批次线表格提交失败");
			}
			rd.setFirstLine(EnrollUtils.getCellFormatValue(row1.getCell(4)));
			rd.setSecondLine(EnrollUtils.getCellFormatValue(row1.getCell(5)));
			rd.setToudangLine(EnrollUtils.getCellFormatValue(row1.getCell(6)));
			rd.setProvinces(c);
			provinceScoreDao.save(rd);
			
		}
		try {
			staticRecruitment.createHtmlForRecruitmentDetail();
		} catch (Exception e) {
			throw new Exception("省份录取批次线页面生成失败，可能数据库中存在不合法字段，具体情况，请联系开发人员调试"
					+ e.toString());
		}
		//DeleteFile.delete(excelPath);
		
	}
	


	
	
	/**
	 * 
	 * 根据artFrom id批量删除
	 * @param ids
	 */
	public void deleteByIds(String id) {
			List<ProvinceScore> l = provinceScoreDao.findBy("provinces.id", id);
		for (ProvinceScore c : l) 
			provinceScoreDao.delete(c);
	}
	
	

	public ProvinceScoreDao getProvinceScoreDao() {
		return provinceScoreDao;
	}
	@Autowired
	public void setProvinceScoreDao(ProvinceScoreDao provinceScoreDao) {
		this.provinceScoreDao = provinceScoreDao;
	}

	public ProvinceManager getProvicneManager() {
		return provicneManager;
	}
	@Autowired
	public void setProvicneManager(ProvinceManager provicneManager) {
		this.provicneManager = provicneManager;
	}

	public ArrayUtils getArrayUtils() {
		return arrayUtils;
	}
	@Autowired
	public void setArrayUtils(ArrayUtils arrayUtils) {
		this.arrayUtils = arrayUtils;
	}

	public StaticRecruitment getStaticRecruitment() {
		return staticRecruitment;
	}
	@Autowired
	public void setStaticRecruitment(StaticRecruitment staticRecruitment) {
		this.staticRecruitment = staticRecruitment;
	}

	public RecruitmentDetailDao getRecruitmentDetailDao() {
		return recruitmentDetailDao;
	}
	@Autowired
	public void setRecruitmentDetailDao(RecruitmentDetailDao recruitmentDetailDao) {
		this.recruitmentDetailDao = recruitmentDetailDao;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
}
